<?php
class DB {
	public $conn;
	public static $sql;
	public static $instance=null;
	private function __construct(){
		$this->conn = mysql_connect(DB_HOST,DB_USER,DB_PASS) or die('Can not connect MySQL Server!');
		mysql_select_db(DB_NAME,$this->conn) or die('Can not select DB!');
		mysql_query('SET NAMES UTF8',$this->conn);		
	}
	public static function getInstance(){
		if(is_null(self::$instance)){
			self::$instance = new DB();
		}
		return self::$instance;
	}
	
	/**
	 * 查询数据库
	 */
	public function select($table,$condition=array(),$field = array(),$index=null){
		$where='';
		if(!empty($condition)){
			
			foreach($condition as $k=>$v){
				$where .= $k."='".$v."' AND ";
			}
			$where = 'WHERE '.$where .'1=1';
		}
		$fieldstr = '';
		if(!empty($field)){
			
			foreach($field as $k=>$v){
				$fieldstr.= $v.',';
			}
			 $fieldstr = rtrim($fieldstr,',');
		}else{
			$fieldstr = '*';
		}
		self::$sql = "SELECT {$fieldstr} FROM {$table} {$where}";
		$result = mysql_query(self::$sql,$this->conn);
		$resultRow = array();
		$i = 0;
		while($row=mysql_fetch_assoc($result)){
			if(!empty($index)){
				$resultRow[$row[$index]] = $row;
			}else{
				$resultRow[$i++] = $row;
			}
		}
		return $resultRow;
	}
	
	public function query($sql){
	    self::$sql = $sql;
	    $result = mysql_query(self::$sql, $this->conn);
	    $resultRow = array();
	    $i = 0;
	    while ($row = mysql_fetch_assoc($result)) {
	    	foreach ($row as $k=>$v){
	    	    $resultRow[$i][$k] = $v;
	    	}
	    	$i++;
	    }
	    return $resultRow;
	}
	
	public function fetchOne($sql){
	    self::$sql = $sql;
	    $result = mysql_query(self::$sql, $this->conn);
	    return mysql_fetch_array($result);
	}
	
	public function exec($sql){
	    self::$sql = $sql;
	    $result = mysql_query(self::$sql, $this->conn);
	    return $result;
	}
	
	/**
	 * 添加一条记录
	 */
	 public function insert($table,$data){
	 	$values = array();
	 	$datas = array();
	 	foreach($data as $k=>$v){
	 		$values[] = "`{$k}`";
	 		$datas[] = "'{$v}'";
	 	}
	 	$values = join(',',$values);
	 	$datas   = join(',',$datas);
	 	self::$sql = "INSERT INTO  {$table} ({$values}) VALUES ({$datas})";
		if(mysql_query(self::$sql)){
			return mysql_insert_id();
		}else{
			return false;
		}
	 }
	 
	 /**
	  * 修改一条记录
	  */
	public function update($table,$data,$condition=array()){
		$where='';
		if(!empty($condition)){
			
			foreach($condition as $k=>$v){
				$where.=$k."='".$v."' AND ";
			}
			$where='WHERE '.$where .'1=1';
		}
		$updatastr = array();
		if(!empty($data)){
			foreach($data as $k=>$v){
				$updatastr[] = "`{$k}`='{$v}'";
			}
			$updatastr = 'SET '.join(',',$updatastr);
		}
		self::$sql = "UPDATE {$table} {$updatastr} {$where}";
		return mysql_query(self::$sql);
	}
	
	/**
	 * 删除记录
	 */
	 public function delete($table,$condition){
	 	$where='';
		if(!empty($condition)){
			
			foreach($condition as $k=>$v){
				$where.=$k."='".$v."' AND ";
			}
			$where='WHERE '.$where .'1=1';
		}
		self::$sql = "DELETE FROM {$table} {$where}";
		return mysql_query(self::$sql);
	 }

	public static function getLastSql(){
		echo self::$sql;
	}
}
?>
